<!--
 * @Description: 商品详情页面组件
 * @Author: hai-27
 * @Date: 2020-02-16 20:20:26
 * @LastEditors: hai-27
 * @LastEditTime: 2020-03-07 21:59:26
 -->
<template>
    <div id="details">
        <!-- 头部 -->
        <div class="page-header">
            <div class="title">
                <p>{{productData.name}}</p>
                <div class="list">
                    <ul>
                        <li>
                            <router-link :to="'/productIntro/' + productData.id">概述</router-link>
                        </li>
                        <li>
                            <router-link to>参数</router-link>
                        </li>
                        <li>
                            <router-link to>用户评价</router-link>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
        <!-- 头部END -->

        <!-- 主要内容 -->
        <div class="main">
            <!-- 左侧商品轮播图 -->
            <div class="block">
                <el-carousel height="560px">
                    <el-carousel-item>
                        <img style="height:560px;" :src="productData.head_img_url">
                    </el-carousel-item>
                </el-carousel>
            </div>
            <!-- 左侧商品轮播图END -->

            <!-- 右侧内容区 -->
            <div class="content">
                <h1 class="name">{{productData['name']}}</h1>
                <p class="intro">{{productData['description']}}</p>
                <p class="store">小米自营</p>
                <div class="price">
                    <span>{{productData.price}}元</span>
                </div>
                <div class="price">
                    <span v-if="productData.stock > 0">库存:{{productData.stock}}</span>
                    <span v-else style="color: red;">库存不足</span>
                </div>
                <div class="pro-list">
                    <span class="pro-name">{{productData.name}}</span>
                    <span class="pro-price">
          </span>
                    <p class="price-sum">总计 :{{productData.price}}元</p>
                </div>
                <!-- 内容区底部按钮 -->
                <div class="button">
                    <el-button class="shop-cart" @click="addCart">加入购物车</el-button>
                    <el-button class="like" @click="collect">收藏</el-button>
                </div>
                <!-- 内容区底部按钮END -->
                <div class="pro-policy">
                    <ul>
                        <li>
                            <i class="el-icon-circle-check"></i> 小米自营
                        </li>
                        <li>
                            <i class="el-icon-circle-check"></i> 小米发货
                        </li>
                        <li>
                            <i class="el-icon-circle-check"></i> 7天无理由退货
                        </li>
                        <li>
                            <i class="el-icon-circle-check"></i> 7天价格保护
                        </li>
                    </ul>
                </div>
            </div>
            <!-- 右侧内容区END -->
        </div>
        <!-- 主要内容END -->
    </div>
</template>
<script>
    export default {
        data() {
            return {
                product_id: null,
                productData: [],  // 商品数据详情
                userInfo: {},  // 用户信息
                user_id: "", // 用户id
            };
        },
        methods: {
            // 添加购物车
            addCart() {
                let json_userInfo = localStorage.getItem('userInfo');
                if (json_userInfo == null) {
                    // 没有登录
                    this.errorNotify('用户未登录','用户未登录');
                }
                else {
                    // 正常登录
                    let userInfo = JSON.parse(json_userInfo);
                    this.userInfo = userInfo; // 绑定数据
                    this.user_id = userInfo.user_id;
                    // 判断库存是否足够
                    if (this.productData.stock <= 0) {
                        // 库存不足
                        this.errorNotify('商品库存不足','商品库存不足');
                        return ;
                    }

                    this.$axios({
                        method: 'post',
                        url: this.baseUrl + '/api/v1/cart/add',
                        headers: {
                            'telephone': userInfo.telephone,
                            'access-token': userInfo.token
                        },
                        data: {
                            productInfo: this.productData
                        }
                    }).then(response => {
                        if (response.data.status == 200) {
                            if (response.data.message != "商品已经添加过了") {
                                // 添加一个购物车数量
                                this.$store.commit('addCartProductNum');
                            }
                            this.notify(response.data.message, response.data.message);
                        }
                    }).catch(error => {
                        console.log(error.response)
                    })
                }
            },
            // 通过商品id 获取商品详情数据
            getProductDetailByID() {
                this.product_id = this.$route.params.id;
                this.$axios.get(this.baseUrl + '/api/v1/product/' + this.$route.params.id)
                    .then(response => {
                        let result = response.data;
                        this.productData = result.data;
                    })
                    .catch(error => {
                        console.log(error)
                    })
            },

            // 收藏商品
            collect() {
                let json_userInfo = localStorage.getItem('userInfo');
                if (json_userInfo == null) {
                    this.errorNotify('用户未登录','用户未登录');
                }
                else {
                    // 正常登录
                    let userInfo = JSON.parse(json_userInfo);
                    this.userInfo = userInfo; // 绑定数据
                    this.user_id = userInfo.user_id;
                    // 发送请求
                    this.$axios({
                        method: 'POST',
                        url: this.baseUrl + '/api/v1/user/collect',
                        data: {
                            'product_id': this.product_id,
                            'user_id': this.user_id
                        },
                        headers: {
                            'access-token': userInfo.token,
                            'telephone': userInfo.telephone
                        }
                    }).then(response => {
                        let result = response.data;
                        if (result.message == "添加收藏成功" && result.status == 200) {
                            const h = this.$createElement;
                            this.$notify({
                                title: '收藏商品成功',
                                message: h('i', {style: 'color: teal'}, '收藏商品成功')
                            });
                        }
                        // 提示已经收藏过了
                        if (result.message == "商品已经收藏过了" && result.status == 2001) {
                            const h = this.$createElement;
                            this.$notify({
                                title: '商品已经收藏过了',
                                message: h('i', {style: 'color: teal'}, '商品已经收藏过了')
                            });
                        }
                    }).catch(error => {

                    })
                }
            },
            // 提示信息 title string 标题 message string 提示信息
            notify(title, message) {
                const h = this.$createElement;
                this.$notify({
                    title: title,
                    type: "success",
                    duration: 2000,
                    message: h('i', {style: 'color: teal'}, message)
                });
            },
            // 错误提示
            errorNotify(title, message) {
                const h = this.$createElement;
                this.$notify({
                    title: title,
                    type: "error",
                    duration: 2000,
                    message: h('i', {style: 'color: red'}, message)
                });
            }

        },
        activated() {
            this.getProductDetailByID();
        }
    };
</script>
<style>
    /* 头部CSS */
    #details .page-header {
        height: 64px;
        margin-top: -20px;
        z-index: 4;
        background: #fff;
        border-bottom: 1px solid #e0e0e0;
        -webkit-box-shadow: 0px 5px 5px rgba(0, 0, 0, 0.07);
        box-shadow: 0px 5px 5px rgba(0, 0, 0, 0.07);
    }
    #details .page-header .title {
        width: 1225px;
        height: 64px;
        line-height: 64px;
        font-size: 18px;
        font-weight: 400;
        color: #212121;
        margin: 0 auto;
    }
    #details .page-header .title p {
        float: left;
    }
    #details .page-header .title .list {
        height: 64px;
        float: right;
    }
    #details .page-header .title .list li {
        float: left;
        margin-left: 20px;
    }
    #details .page-header .title .list li a {
        font-size: 14px;
        color: #616161;
    }
    #details .page-header .title .list li a:hover {
        font-size: 14px;
        color: #ff6700;
    }
    /* 头部CSS END */

    /* 主要内容CSS */
    #details .main {
        width: 1225px;
        height: 560px;
        padding-top: 30px;
        margin: 0 auto;
    }
    #details .main .block {
        float: left;
        width: 560px;
        height: 560px;
    }
    #details .el-carousel .el-carousel__indicator .el-carousel__button {
        background-color: rgba(163, 163, 163, 0.8);
    }
    #details .main .content {
        float: left;
        margin-left: 25px;
        width: 640px;
    }
    #details .main .content .name {
        height: 30px;
        line-height: 30px;
        font-size: 24px;
        font-weight: normal;
        color: #212121;
    }
    #details .main .content .intro {
        color: #b0b0b0;
        padding-top: 10px;
    }
    #details .main .content .store {
        color: #ff6700;
        padding-top: 10px;
    }
    #details .main .content .price {
        display: block;
        font-size: 18px;
        color: #ff6700;
        border-bottom: 1px solid #e0e0e0;
        padding: 25px 0 25px;
    }
    #details .main .content .price .del {
        font-size: 14px;
        margin-left: 10px;
        color: #b0b0b0;
        text-decoration: line-through;
    }
    #details .main .content .pro-list {
        background: #f9f9fa;
        padding: 30px 60px;
        margin: 50px 0 50px;
    }
    #details .main .content .pro-list span {
        line-height: 30px;
        color: #616161;
    }
    #details .main .content .pro-list .pro-price {
        float: right;
    }
    #details .main .content .pro-list .pro-price .pro-del {
        margin-left: 10px;
        text-decoration: line-through;
    }
    #details .main .content .pro-list .price-sum {
        color: #ff6700;
        font-size: 24px;
        padding-top: 20px;
    }
    #details .main .content .button {
        height: 55px;
        margin: 10px 0 20px 0;
    }
    #details .main .content .button .el-button {
        float: left;
        height: 55px;
        font-size: 16px;
        color: #fff;
        border: none;
        text-align: center;
    }
    #details .main .content .button .shop-cart {
        width: 340px;
        background-color: #ff6700;
    }
    #details .main .content .button .shop-cart:hover {
        background-color: #f25807;
    }

    #details .main .content .button .like {
        width: 260px;
        margin-left: 40px;
        background-color: #b0b0b0;
    }
    #details .main .content .button .like:hover {
        background-color: #757575;
    }
    #details .main .content .pro-policy li {
        float: left;
        margin-right: 20px;
        color: #b0b0b0;
    }
    /* 主要内容CSS END */
</style>
